<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>后台管理</title>
	<link rel="stylesheet" th:href="@{/static/layui/css/layui.css}" media="all"/>
	<link rel="stylesheet" th:href="@{/static/css/thesame.css}" media="all"/>
	<link rel="stylesheet" th:href="@{/static/css/tree.css}" media="all"/>
	<link rel="stylesheet" th:href="@{/static/css/page.css}" media="all"/>
	<link rel="stylesheet" th:href="@{/static/css/popCss.css}" media="all"/>
	<link rel="stylesheet" th:href="@{/static/user/css/user.css}"  media="all"/>
    <style type="text/css">
        /*取消选中效果*/
		div{
	    -moz-user-select:none;/*火狐*/
	    -webkit-user-select:none;/*webkit浏览器*/
	    -ms-user-select:none;/*IE10*/
	    -khtml-user-select:none;/*早期浏览器*/
	      user-select:none;
		}
    </style>
</head>
<script th:inline="javascript">var basePath = [[${#httpServletRequest.getContextPath()}]];</script>
<body>
<div class="bgWhite box-shadows1 border-radius4 scroll-box">
	<div class="scroll-box-con border-radius4 bgWhite">
		<div class="border-bot1px pb20px mb20px">
			<div class="add-mess add-mess-con add-mess-con-width overflow">
				<div class="add-mess-child">
					<div>
						<label>用户姓名：</label>
					</div>
					<div class="add-mess-form">
						<p>
							<input type="text" id="userName" placeholder="请输入">
						</p>
					</div>
				</div>
				<div class="add-mess-child">
					<div>
						<label>登录账号：</label>
					</div>
					<div class="add-mess-form">
						<p>
							<input type="text" id="loginName" placeholder="请输入">
						</p>
					</div>
				</div>
				<div class="add-mess-child">
					<div>
						<label>手机号码：</label>
					</div>
					<div class="add-mess-form">
						<p>
							<input type="text" id="phone" placeholder="请输入">
						</p>
					</div>
				</div>
				<div class="add-mess-child oveflow">
					<span class="white border-radius4 bgGreen left pl20px pr20px ml30px lh34px" style="cursor: pointer;" id="queryBtn">查&nbsp;询</span>
					<span class="border-radius4 border1px-ddd left pl20px pr20px ml30px lh32px" style="cursor: pointer;" id="resetBtn">重&nbsp;置</span>
				</div>
			</div>

		</div>
		<div class="mb20px lh32px overflow add-btn-input">
			<p class="left cursor">
				<span class="white border-radius4 bgGreen inline-block pl15px pr15px" onclick="addUser()"><img th:src="@{/static/img/button_add.png}" class="mr5px" style="vertical-align: -1px;">新建</span>
			</p>
		</div>
		<div class="border1px border-radius4 relative mb40px">
			<table class="layui-hide" id="test" lay-filter="test"></table>
		</div>
	</div>
</div>

<script type="text/javascript" th:src="@{/static/js/jquery-2.1.3.min.js}" charset="utf-8"></script>
<script type="text/javascript" th:src="@{/static/js/layuiCon.js}" charset="utf-8"></script>
<script type="text/javascript" th:src="@{/static/layui/layui.js}" charset="utf-8"></script>
<script type="text/javascript" th:src="@{/static/js/date.js}" charset="utf-8"></script>
<script type="text/javascript" th:src="@{/static/js/treeTwo.js}" charset="utf-8"></script>
<script>

    $(function(){
    	//查询初始化
		tableGrenate();
    })

    /**
     * table query
     */
    function tableGrenate(){
        layui.use(['table'], function() {
            var element = layui.element;
            table = layui.table;
            var userName=$("#userName").val();
            var loginName=$("#loginName").val();
            var phone=$("#phone").val();
            //表格
            table.render({
                elem: '#test'
                ,url: '../user/getUserList'
                ,where:{"userName":userName,"loginName":loginName,"phone":phone,"postType":"1"}
                ,page: true //开启分页
                ,limit: 10
                ,limits: [5,10,20]
                ,cols: [
                    [ //表头
                        {field:'LOGIN_NAME', title:'登录账号', width:130,},
                        {field:'USER_NAME', title:'用户姓名', width:130,},
                        {field:'ORG_NAME', title:'所属组织', width:130,},
                        {field:'POST', title:'部门/职务', width:130,},
                        {field:'PHONE', title:'手机号码', width:150,},
                        {field:'LOGIN_TIME', title:'最近登录时间', width:180,},
                        {field:'STATUS', title:'状态', width:80,
                            templet: function(res){
                                var status = res.STATUS;
                                var html = status==0?html = "<em style=\"color:red;font-style: inherit;\">"+res.STATUS_NAME+"</em>":res.STATUS_NAME;
                                return html;
                            }
                        },
                        {field: 'operation',title: '操作',style: 'color:#1C9399',fixed: 'right',
                            templet: function(v){
								var html='<div style="margin-left: 12px;">';
                                html +='<span class=\'queryUser\' style=\"cursor: pointer;\"  data-id=\''+v.USER_NO+'\'>查看</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class=\'editUser\' style=\"cursor: pointer;\" data-id=\''+v.USER_NO+'\'>编辑&nbsp;&nbsp;&nbsp;&nbsp;</span>';
                                if(v.STATUS=='1' && v.LOGIN_TIME==null){
                                    html +="<span class='deleteUser'  style=\"cursor: pointer;\"  data-id='"+v.USER_NO+"'>删除&nbsp;&nbsp;&nbsp;&nbsp;</span>";
                                }else if(v.STATUS=='0' && v.LOGIN_TIME!=null){
                                    html +="<span class='openUser' style=\"cursor: pointer;\"  data-id='"+v.USER_NO+"'>开通&nbsp;&nbsp;&nbsp;&nbsp;</span>";
                                }else if(v.STATUS=='1' && v.LOGIN_TIME!=null){
                                    html +="<span class='closeUser' style=\"cursor: pointer;\"data-id='"+v.USER_NO+"'>停用&nbsp;&nbsp;&nbsp;&nbsp;</span>";
                                }
								html +="<span class='resetpswUser' style=\"cursor: pointer;\"data-id='"+v.USER_NO+"'>重置密码&nbsp;&nbsp;&nbsp;&nbsp;</span>";
                                html +='</div>';
                                return html;
                            }
                        }
                    ]
                ]
                ,done: function(res, curr, count){
                    //查看按钮初始化
                    $("span.queryUser").unbind().on("click",function (e) {
						var id = $(this).data("id");
						var args=[];
						args.push(id);
						queryUser(id);
                    })
                    //编辑按钮初始化
                    $("span.editUser").unbind().on("click",function (e) {
						var id = $(this).data("id");
						var args=[];
						args.push(id);
						editUser(id);
                    })
                    //删除按钮初始化
                    $("span.deleteUser").unbind().on("click",function (e) {
                        var id = $(this).data("id");
                        var args=[];
                        args.push(id);
                        deleteCon("",deleteUser, args, 1);
                    })
                    //开通按钮初始化
                    $("span.openUser").unbind().on("click",function (e) {
                        var id = $(this).data("id");
                        var args=[];
                        args.push(id);
                        confirmCon("确认要开通吗？",openUser, args, 1);
                    })
                    //停用按钮初始化
                    $("span.closeUser").unbind().on("click",function (e) {
                        var id = $(this).data("id");
                        var args=[];
                        args.push(id);
                        confirmCon("确认要停用吗？",closeUser, args, 1);
                    })
					//重置密码初始化
					$("span.resetpswUser").unbind().on("click",function (e) {
						var id = $(this).data("id");
						var args=[];
						args.push(id);
						confirmCon("确认要重置密码吗？",resetpswUser, args, 1);
					})
                }
            });
        });
    }

	/**
	 * 查询按钮点击
	 */
    $("#queryBtn").unbind().click(function(){
		tableGrenate();
    })

    /**
	 * 重置按钮点击
	 */
    $("#resetBtn").unbind().click(function(){
    	$("#userName").val(null).html("");
    	$("#loginName").val(null).html("");
    	$("#phone").val(null).html("");

    })

    function getUserById(id,scope) {
        $.ajax({
            url:basePath+"/user/getUserById",
            type:"post",
            dataType:"json",
            data:{"userNo":id},
            success:function (result) {
                if(result.code == 200){
                    $("input[name=userNo]",scope).val(result.data.USER_NO);
                    $("input[name=loginName]",scope).val(result.data.LOGIN_NAME);
                    $("input[name=loginName]",scope).attr("readonly","readonly");
                    $("input[name=userName]",scope).val(result.data.USER_NAME);
                    $("input[name=phone]",scope).val(result.data.PHONE);
                    $("input[name=post]",scope).val(result.data.POST);
                    if(result.data.POST_TYPE == "1" || result.data.POST_TYPE == 1) {
						$("#selectParentOrg").val(result.data.ORG_ID).text(result.data.ORG_NAME);
					}else{
						$("#selectParentOrg").val(result.data.ORG_ID).trigger("change");
						if(result.data.regions && result.data.regions.length > 0){
							$.each(result.data.regions,function (i,v) {
								$("span#regions_"+v.region).removeClass("noSelect").addClass("current");
							})
							$("span.wyfzrRegion:not(.noSelect)").unbind().on("click",function (e) {
								if($(this).hasClass("current")){
									$(this).removeClass("current");
								}else{
									$(this).addClass("current");
								}
							})
						}
                    }
                    $("#userSave").html("修改");
                }else{
                    layer.closeAll();
                    layer.msg(result.msg);
                }
            }
        })
    }

    // function resetPassword(id) {
    //     $.ajax({
    //         url:basePath+"/user/resetPassword",
    //         type:"post",
    //         dataType:"json",
    //         data:{"userNo":id},
    //         success:function (result) {
    //             layer.msg(result.msg);
    //             if(result.code == 200){
    //                 getData(currentPage)
    //             }
    //         }
    //     })
    // }

	/**
	 * 删除用户
	 */
    function deleteUser(id) {
    	var userType = $("li.userType span.current").data('val');
        $.ajax({
            url:basePath+"/user/delete",
            type:"post",
            dataType:"json",
            data:{"userNo":id,"postType":userType},
            success:function (result) {
                if(result.code == 200){
					layerResult("删除成功",1);
                }else{
					layerResult("删除失败",2);
                }
            }
        })
    }

	/**
	 * 开通用户
	 */
	function openUser(id) {
        $.ajax({
            url:basePath+"/user/open",
            type:"post",
            dataType:"json",
            data:{"userNo":id},
            success:function (result) {
                if(result.code == 200){
					layerResult("启用成功",1);
                }else{
					layerResult("启用失败",2);
                }
            }
        })
    }

    /**
	 * 关闭用户
	 */
    function closeUser(id,userType) {
        $.ajax({
            url:basePath+"/user/close",
            type:"post",
            dataType:"json",
            data:{"userNo":id,"postType":userType},
            success:function (result) {
                if(result.code == 200){
					layerResult("停用成功",1);
                }else{
					layerResult("停用失败",2);
                }
            }
        })
    }

	/**
	 * 重置用户密码
	 */
	function resetpswUser(id) {
		$.ajax({
			url:basePath+"/user/resetPassword",
			type:"post",
			dataType:"json",
			data:{"userNo":id},
			success:function (result) {
				if(result.code == 200){
					layerResult("重置密码成功",1);
				}else{
					layerResult("重置密码失败",2);
				}
			}
		})
	}

    /**
	 * 判断是否
	 */
    function isNull(value) {
        return value == null || value === "" || value === undefined;
    }

	/**
	 * 跳转到添加用户
	 */
	function addUser(){
		window.location.href=basePath+"/user/addPage";
	}

	/**
	 * layer msg
	 */
	function layerMsg(msg){
		var layer;
		layui.use(['layer'], function() {
			layer = layui.layer;
			layer.ready(function(){
				layer.msg(msg);
			});
		});
	}

	/**
	 * layer result
	 */
	function layerResult(msg,type){
		var layer;
		layui.use(['layer'], function() {
			layer = layui.layer;
			layer.ready(function(){
				layer.msg(msg, {icon: type, time:1500},function(){
					$(".layui-laypage-btn").click();
				});
			})
		});
	}

	/**
	 * 跳转到修改用户
	 */
	function editUser(userNo){
		window.location.href=basePath+"/user/editPage?userNo="+userNo;
	}

	/**
	 * 跳转到查看用户
	 */
	function queryUser(userNo){
		window.location.href=basePath+"/user/infoPage?userNo="+userNo;
	}

</script>
</body>
</html>
